package com.example;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Created by v_chenqi02 on 2016/11/17.
 */

@Service
public class ComputerServiceImpl implements ComputerService {

    Logger log = LoggerFactory.getLogger(ComputerServiceImpl.class);

    @Autowired
    RestTemplate template;

    @HystrixCommand(fallbackMethod = "fallBack")
    @Override
    public String add(int a, int b) {
//        return template.getForEntity("http://service-a/add?a="+a+"&b="+b, String.class).getBody();
        return template.getForEntity("http://localhost:5555/api-a/add?a="+a+"&b="+b, String.class).getBody();
    }

    @Override
    public String fallBack(int a, int b) {
        log.error("调用发生错误了，参数a="+a+"\t参数b="+b);
        return "error";
    }

    @HystrixCommand(fallbackMethod = "fallBack")
    @Override
    public String del(int a, int b) {
        return template.getForEntity("http://localhost:5555/api-b/del?a="+a+"&b="+b, String.class).getBody();
    }

    @HystrixCommand(fallbackMethod = "fallBack")
    @Override
    public String addNextdel(int a, int b) {
        String addResult = add(a,b);
        String delResult = del(a,b);

        return "add:"+addResult+"\tdel:"+delResult;
    }
}
